﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.IO
{
    public static class PathExtension
    {
        public static string GetAlternativeFilePath(string path)
        {
            if (File.Exists(path))
            {
                string dir = Path.GetDirectoryName(path),
                    filename = Path.GetFileNameWithoutExtension(path),
                    ext = Path.GetExtension(path),
                    res = "";
                for (int i = 1; i < int.MaxValue; i++)                
                    if (!File.Exists(res = Path.Combine(dir, "{0}({1}){2}".goFormat(filename, i, ext))))
                        break;
                return res;
            }
            else
                return path;

        }

        public static string CorrectFileName(string path)
        {
            return new String((path ?? "").ToCharArray().Where(s => !NotAllowFilenameChars.Contains(s)).ToArray());
        }

        public static string CorrectPath(string path)
        {
            return new String((path ?? "").ToCharArray().Where(s => !NotAllowPathChars.Contains(s)).ToArray());
        }

        private readonly static HashSet<char> NotAllowFilenameChars = new HashSet<char>(Path.GetInvalidFileNameChars());
        private readonly static HashSet<char> NotAllowPathChars = new HashSet<char>(Path.GetInvalidPathChars());
            //"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZабвгдеёжзийклмнопрстуфхцчщшьыъэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧЩШЬЫЪЭЮЯ01234567890_-+.()[]{}<>'@`~!@#№;$^&");
    }
}
